package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.Constants;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.mapper.SysUserMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVO;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Override
    public PageResponse<NursingProjectVO> pageQuery(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProject> nursingProjects=nursingProjectMapper.selectByNameAndStatus(name,status);
        List<NursingProjectVO> collect = nursingProjects.getResult().stream().map(nursingProject -> {
            NursingProjectVO nursingProjectVO = new NursingProjectVO();
            BeanUtils.copyProperties(nursingProject, nursingProjectVO);
            Integer count = nursingProjectPlanMapper.selectCountPlanByProjectId(nursingProject.getId());
            nursingProjectVO.setCount(count);
            String creator = sysUserMapper.selectRealNameById(nursingProject.getCreateBy());
            nursingProjectVO.setCreator(creator);
            return nursingProjectVO;
        }).collect(Collectors.toList());
        PageResponse<NursingProjectVO> pageResponse = new PageResponse<>();
        pageResponse.setPage(nursingProjects.getPageNum());
        pageResponse.setTotal(nursingProjects.getTotal());
        pageResponse.setRecords(collect);
        pageResponse.setPages((long) nursingProjects.getPages());
        pageResponse.setPageSize(nursingProjects.getPageSize());
        return pageResponse;
    }

    @Override
    public void addNursingProject(NursingProject nursingProject) {
        //校验项目名称是否为空或者存在
        if(Strings.isBlank(nursingProject.getName())){
            throw new BaseException(BasicEnum.NURSING_PROJECT_IS_EXIST);
        }
        NursingProject nursingProjectInDb=nursingProjectMapper.selectByName(nursingProject.getName());
        if(Objects.nonNull(nursingProjectInDb)){
            throw new BaseException(BasicEnum.NURSING_PROJECT_IS_EXIST);
        }
        nursingProjectMapper.insert(nursingProject);
    }

    @Override
    public NursingProjectVO getNursingProject(Long id) {
        //首先根据id查询出基本的信息
        NursingProject nursingProject= nursingProjectMapper.selectById(id);
        NursingProjectVO nursingProjectVO = new NursingProjectVO();
        BeanUtils.copyProperties(nursingProject, nursingProjectVO);
        nursingProjectVO.setCount(nursingProjectPlanMapper.selectCountPlanByProjectId(id));
        return nursingProjectVO;
    }

    @Override
    public void updateNursingProject(NursingProject nursingProject) {
        Integer count = nursingProjectPlanMapper.selectCountPlanByProjectId(nursingProject.getId());
        if(count >0){
            throw new BaseException(BasicEnum.NURSING_PROJECT_BINDING_PLAN);
        }
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void startOrStopStatus(Long id, Integer status) {
        if(status.equals(Constants.STATUS_ZERO)){
            Integer count = nursingProjectPlanMapper.selectCountPlanByProjectId(id);
            if(count >0){
                throw new BaseException(BasicEnum.NURSING_PROJECT_BINDING_PLAN);
            }
        }
        NursingProject nursingProject= new NursingProject();
        nursingProject.setId(id);
        nursingProject.setStatus(status);
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void deleteNursingProject(Long id) {
        Integer count = nursingProjectPlanMapper.selectCountPlanByProjectId(id);
        if(count >0){
            throw new BaseException(BasicEnum.NURSING_PROJECT_BINDING_PLAN);
        }
        nursingProjectMapper.delete(id);
    }

    @Override
    public List<NursingProjectVO> getAllNursingProject() {
        List<NursingProject> nursingProjects=nursingProjectMapper.selectAll();
      return   nursingProjects.stream().map(nursingProject -> {
            NursingProjectVO nursingProjectVO = new NursingProjectVO();
            BeanUtils.copyProperties(nursingProject, nursingProjectVO);
          Integer count = nursingProjectPlanMapper.selectCountPlanByProjectId(nursingProject.getId());
          nursingProjectVO.setCount(count);
          String creator=sysUserMapper.selectRealNameById(nursingProject.getCreateBy());
            nursingProjectVO.setCreator(creator);
            return nursingProjectVO;
        }).collect(Collectors.toList());
    }
}
